package o;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Rect;
import android.util.SparseBooleanArray;
import android.util.TimingLogger;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public final class lz {
    static final e ˏ = new e() {
        public boolean ˊ(int i, float[] fArr) {
            return (ॱ(fArr) || ˋ(fArr) || ˏ(fArr)) ? false : true;
        }

        private boolean ˋ(float[] fArr) {
            return fArr[2] <= 0.05f;
        }

        private boolean ॱ(float[] fArr) {
            return fArr[2] >= 0.95f;
        }

        private boolean ˏ(float[] fArr) {
            return fArr[0] >= 10.0f && fArr[0] <= 37.0f && fArr[1] <= 0.82f;
        }
    };
    private final List<b> ˊ;
    private final SparseBooleanArray ˋ = new SparseBooleanArray();
    private final Map<lx, b> ˎ = new ds();
    private final List<lx> ॱ;
    @Nullable
    private final b ॱॱ = ˊ();

    public interface e {
        boolean ˊ(@ColorInt int i, @NonNull float[] fArr);
    }

    public static final class a {
        @Nullable
        private Rect ʻ;
        @Nullable
        private final Bitmap ˊ;
        private final List<lx> ˋ = new ArrayList();
        private int ˎ = 16;
        private int ˏ = 12544;
        @Nullable
        private final List<b> ॱ;
        private final List<e> ॱॱ = new ArrayList();
        private int ᐝ = -1;

        public a(@NonNull Bitmap bitmap) {
            if (bitmap == null || bitmap.isRecycled()) {
                throw new IllegalArgumentException("Bitmap is not valid");
            }
            this.ॱॱ.add(lz.ˏ);
            this.ˊ = bitmap;
            this.ॱ = null;
            this.ˋ.add(lx.ˎ);
            this.ˋ.add(lx.ˊ);
            this.ˋ.add(lx.ˏ);
            this.ˋ.add(lx.ˋ);
            this.ˋ.add(lx.ॱ);
            this.ˋ.add(lx.ॱॱ);
        }

        @NonNull
        public a ˋ(int i) {
            this.ˎ = i;
            return this;
        }

        @NonNull
        public a ॱ() {
            this.ॱॱ.clear();
            return this;
        }

        @NonNull
        public a ˎ(e eVar) {
            if (eVar != null) {
                this.ॱॱ.add(eVar);
            }
            return this;
        }

        @NonNull
        public lz ˎ() {
            List ˊ;
            TimingLogger timingLogger = null;
            if (this.ˊ != null) {
                e[] eVarArr;
                Bitmap ˏ = ˏ(this.ˊ);
                if (timingLogger != null) {
                    timingLogger.addSplit("Processed Bitmap");
                }
                Rect rect = this.ʻ;
                if (!(ˏ == this.ˊ || rect == null)) {
                    double width = ((double) ˏ.getWidth()) / ((double) this.ˊ.getWidth());
                    rect.left = (int) Math.floor(((double) rect.left) * width);
                    rect.top = (int) Math.floor(((double) rect.top) * width);
                    rect.right = Math.min((int) Math.ceil(((double) rect.right) * width), ˏ.getWidth());
                    rect.bottom = Math.min((int) Math.ceil(width * ((double) rect.bottom)), ˏ.getHeight());
                }
                int[] ˎ = ˎ(ˏ);
                int i = this.ˎ;
                if (this.ॱॱ.isEmpty()) {
                    eVarArr = timingLogger;
                } else {
                    eVarArr = (e[]) this.ॱॱ.toArray(new e[this.ॱॱ.size()]);
                }
                ma maVar = new ma(ˎ, i, eVarArr);
                if (ˏ != this.ˊ) {
                    ˏ.recycle();
                }
                ˊ = maVar.ˊ();
                if (timingLogger != null) {
                    timingLogger.addSplit("Color quantization completed");
                }
            } else if (this.ॱ != null) {
                ˊ = this.ॱ;
            } else {
                throw new AssertionError();
            }
            lz lzVar = new lz(ˊ, this.ˋ);
            lzVar.ˏ();
            if (timingLogger != null) {
                timingLogger.addSplit("Created Palette");
                timingLogger.dumpToLog();
            }
            return lzVar;
        }

        private int[] ˎ(Bitmap bitmap) {
            int i = 0;
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            Object obj = new int[(width * height)];
            bitmap.getPixels(obj, 0, width, 0, 0, width, height);
            if (this.ʻ == null) {
                return obj;
            }
            int width2 = this.ʻ.width();
            int height2 = this.ʻ.height();
            Object obj2 = new int[(width2 * height2)];
            while (i < height2) {
                System.arraycopy(obj, ((this.ʻ.top + i) * width) + this.ʻ.left, obj2, i * width2, width2);
                i++;
            }
            return obj2;
        }

        private Bitmap ˏ(Bitmap bitmap) {
            double d = -1.0d;
            int width;
            if (this.ˏ > 0) {
                width = bitmap.getWidth() * bitmap.getHeight();
                if (width > this.ˏ) {
                    d = Math.sqrt(((double) this.ˏ) / ((double) width));
                }
            } else if (this.ᐝ > 0) {
                width = Math.max(bitmap.getWidth(), bitmap.getHeight());
                if (width > this.ᐝ) {
                    d = ((double) this.ᐝ) / ((double) width);
                }
            }
            return d <= 0.0d ? bitmap : Bitmap.createScaledBitmap(bitmap, (int) Math.ceil(((double) bitmap.getWidth()) * d), (int) Math.ceil(d * ((double) bitmap.getHeight())), false);
        }
    }

    public static final class b {
        private int ʻ;
        private int ʼ;
        @Nullable
        private float[] ʽ;
        private final int ˊ;
        private final int ˋ;
        private final int ˎ;
        private final int ˏ;
        private final int ॱ;
        private boolean ᐝ;

        public b(@ColorInt int i, int i2) {
            this.ˊ = Color.red(i);
            this.ˏ = Color.green(i);
            this.ˎ = Color.blue(i);
            this.ˋ = i;
            this.ॱ = i2;
        }

        @ColorInt
        public int ˏ() {
            return this.ˋ;
        }

        @NonNull
        public float[] ॱ() {
            if (this.ʽ == null) {
                this.ʽ = new float[3];
            }
            fu.ˊ(this.ˊ, this.ˏ, this.ˎ, this.ʽ);
            return this.ʽ;
        }

        public int ˊ() {
            return this.ॱ;
        }

        @ColorInt
        public int ˎ() {
            ʻ();
            return this.ʼ;
        }

        @ColorInt
        public int ˋ() {
            ʻ();
            return this.ʻ;
        }

        private void ʻ() {
            if (!this.ᐝ) {
                int ˊ = fu.ˊ(-1, this.ˋ, 4.5f);
                int ˊ2 = fu.ˊ(-1, this.ˋ, 3.0f);
                if (ˊ == -1 || ˊ2 == -1) {
                    int ˊ3 = fu.ˊ(-16777216, this.ˋ, 4.5f);
                    int ˊ4 = fu.ˊ(-16777216, this.ˋ, 3.0f);
                    if (ˊ3 == -1 || ˊ4 == -1) {
                        if (ˊ != -1) {
                            ˊ = fu.ˋ(-1, ˊ);
                        } else {
                            ˊ = fu.ˋ(-16777216, ˊ3);
                        }
                        this.ʻ = ˊ;
                        if (ˊ2 != -1) {
                            ˊ = fu.ˋ(-1, ˊ2);
                        } else {
                            ˊ = fu.ˋ(-16777216, ˊ4);
                        }
                        this.ʼ = ˊ;
                        this.ᐝ = true;
                        return;
                    }
                    this.ʻ = fu.ˋ(-16777216, ˊ3);
                    this.ʼ = fu.ˋ(-16777216, ˊ4);
                    this.ᐝ = true;
                    return;
                }
                this.ʻ = fu.ˋ(-1, ˊ);
                this.ʼ = fu.ˋ(-1, ˊ2);
                this.ᐝ = true;
            }
        }

        public String toString() {
            return new StringBuilder(getClass().getSimpleName()).append(" [RGB: #").append(Integer.toHexString(ˏ())).append(']').append(" [HSL: ").append(Arrays.toString(ॱ())).append(']').append(" [Population: ").append(this.ॱ).append(']').append(" [Title Text: #").append(Integer.toHexString(ˎ())).append(']').append(" [Body Text: #").append(Integer.toHexString(ˋ())).append(']').toString();
        }

        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null || getClass() != obj.getClass()) {
                return false;
            }
            b bVar = (b) obj;
            if (this.ॱ == bVar.ॱ && this.ˋ == bVar.ˋ) {
                return true;
            }
            return false;
        }

        public int hashCode() {
            return (this.ˋ * 31) + this.ॱ;
        }
    }

    @NonNull
    public static a ˎ(@NonNull Bitmap bitmap) {
        return new a(bitmap);
    }

    lz(List<b> list, List<lx> list2) {
        this.ˊ = list;
        this.ॱ = list2;
    }

    @NonNull
    public List<b> ˎ() {
        return Collections.unmodifiableList(this.ˊ);
    }

    void ˏ() {
        int size = this.ॱ.size();
        for (int i = 0; i < size; i++) {
            lx lxVar = (lx) this.ॱ.get(i);
            lxVar.ˋॱ();
            this.ˎ.put(lxVar, ॱ(lxVar));
        }
        this.ˋ.clear();
    }

    @Nullable
    private b ॱ(lx lxVar) {
        b ˏ = ˏ(lxVar);
        if (ˏ != null && lxVar.ॱॱ()) {
            this.ˋ.append(ˏ.ˏ(), true);
        }
        return ˏ;
    }

    @Nullable
    private b ˏ(lx lxVar) {
        float f = 0.0f;
        b bVar = null;
        int size = this.ˊ.size();
        int i = 0;
        while (i < size) {
            float f2;
            b bVar2 = (b) this.ˊ.get(i);
            if (ˎ(bVar2, lxVar)) {
                float ˋ = ˋ(bVar2, lxVar);
                if (bVar == null || ˋ > f) {
                    f2 = ˋ;
                    i++;
                    f = f2;
                    bVar = bVar2;
                }
            }
            bVar2 = bVar;
            f2 = f;
            i++;
            f = f2;
            bVar = bVar2;
        }
        return bVar;
    }

    private boolean ˎ(b bVar, lx lxVar) {
        float[] ॱ = bVar.ॱ();
        if (ॱ[1] < lxVar.ˎ() || ॱ[1] > lxVar.ˏ() || ॱ[2] < lxVar.ॱ() || ॱ[2] > lxVar.ʼ() || this.ˋ.get(bVar.ˏ())) {
            return false;
        }
        return true;
    }

    private float ˋ(b bVar, lx lxVar) {
        float abs;
        float abs2;
        float f = 0.0f;
        float[] ॱ = bVar.ॱ();
        int ˊ = this.ॱॱ != null ? this.ॱॱ.ˊ() : 1;
        if (lxVar.ʻ() > 0.0f) {
            abs = (1.0f - Math.abs(ॱ[1] - lxVar.ˋ())) * lxVar.ʻ();
        } else {
            abs = 0.0f;
        }
        if (lxVar.ᐝ() > 0.0f) {
            abs2 = (1.0f - Math.abs(ॱ[2] - lxVar.ˊ())) * lxVar.ᐝ();
        } else {
            abs2 = 0.0f;
        }
        if (lxVar.ʽ() > 0.0f) {
            f = lxVar.ʽ() * (((float) bVar.ˊ()) / ((float) ˊ));
        }
        return (abs + abs2) + f;
    }

    @Nullable
    private b ˊ() {
        int i = Integer.MIN_VALUE;
        b bVar = null;
        int size = this.ˊ.size();
        int i2 = 0;
        while (i2 < size) {
            int ˊ;
            b bVar2 = (b) this.ˊ.get(i2);
            if (bVar2.ˊ() > i) {
                ˊ = bVar2.ˊ();
            } else {
                bVar2 = bVar;
                ˊ = i;
            }
            i2++;
            i = ˊ;
            bVar = bVar2;
        }
        return bVar;
    }
}
